package org.example;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class Swing_del {
    public static void main() throws IOException {
        UserDaoImpl userDao= new UserDaoImpl();
        //生成对象

        //创建了一个窗口  标题为四川师范大学管理系统
        JFrame jFrame=new JFrame("学生管理系统");
        //设置了窗口大小
        jFrame.setSize(800,600);
        //设置窗口可见，用true和false表示
        jFrame.setVisible(true);
        //图标对象来自于哪里   从demo2获取1.jpg图片
        InputStream tupian = Demo2.class.getClassLoader().getResourceAsStream("1.jpg");
        //把他转为图片对象 image类型
        BufferedImage image= ImageIO.read(tupian);
        //设置窗口图标
        jFrame.setIconImage(image);
        //完全退出，非关闭后悄悄运行
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
        //居中显示
        jFrame.setLocationRelativeTo(null);


        //系统标题
        JLabel title =new JLabel("学生删除系统",JLabel.CENTER);
        title.setPreferredSize(new Dimension(0,80));
        title.setFont(new Font("华文行楷",Font.BOLD,40));

        //通用文字
        Font font =new Font("楷体",Font.PLAIN,20);
        //账号密码文字对象
        JLabel LabelUser = new JLabel("ID:");
        JLabel LabePass = new JLabel("学生姓名:");
        LabelUser.setFont(font);
        LabePass.setFont(font);//用法和前面一样的

        //按钮
        JButton save=new JButton("确认删除");
        save.setFont(font);
        JButton quit =new JButton("返回上一级");
        quit.setFont(font);

        //输入框
        JTextField username = new JTextField();
        JTextField password = new JTextField();
        //输入框高
        username.setPreferredSize(new Dimension(200,30));
        password.setPreferredSize(new Dimension(200,30));

        SpringLayout layout = new SpringLayout();
        //中间面板  放置输入用户和密码部分
        JPanel center = new JPanel(layout);
        int usernameW = Spring.width(LabelUser).getValue();
        int usernameH = Spring.height(LabelUser).getValue();
        int sum = (usernameW+Spring.width(username).getValue()+20)/2;
        layout.putConstraint(SpringLayout.WEST,LabelUser,-sum,SpringLayout.HORIZONTAL_CENTER,center);
        layout.putConstraint(SpringLayout.WEST,username,usernameW+20,SpringLayout.WEST,LabelUser);

        layout.putConstraint(SpringLayout.NORTH,LabePass,usernameH+30,SpringLayout.NORTH,LabelUser);
        layout.putConstraint(SpringLayout.WEST,LabePass,-45,SpringLayout.WEST,LabelUser);

        layout.putConstraint(SpringLayout.NORTH,password,0,SpringLayout.NORTH,LabePass);
        layout.putConstraint(SpringLayout.WEST,password,0,SpringLayout.WEST,username);

        //按钮位置
        layout.putConstraint(SpringLayout.NORTH,save,90,SpringLayout.NORTH,LabePass);
        layout.putConstraint(SpringLayout.WEST,save,-30,SpringLayout.WEST,LabelUser);

        layout.putConstraint(SpringLayout.NORTH,quit,90,SpringLayout.NORTH,LabePass);
        layout.putConstraint(SpringLayout.WEST,quit,200,SpringLayout.WEST,save);


        center.add(LabelUser);
        center.add(username);
        center.add(LabePass);
        center.add(password);
        center.add(save);
        center.add(quit);

        //1.对按钮的点击事件  (这里的代码是用户点击后触发的)
        save.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int ID = Integer.parseInt(username.getText());
                String Name = password.getText();//横线代表 这个方法过期了  有好的 替代
                boolean b= false;

                try {
                    String hua=userDao.Student_name(ID,Name);
                    try {
                        b = userDao.Delete(ID,Name);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(ex);
                    }
                    if(b){
                        JOptionPane.showMessageDialog(jFrame,"存在该学生："+hua);
                        JOptionPane.showMessageDialog(jFrame,"删除成功！");
                        System.out.println("删除成功");
                        jFrame.setVisible(true);

                    }
                    else{
                        System.out.println("未找到该学生");
                        JOptionPane.showMessageDialog(jFrame,"未找到该学生！请重新输入！");
                    }
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                System.out.println(ID + Name);

            }
        });


        //把最开始的标题放在北边
        jFrame.add(title,BorderLayout.NORTH);
        //把输入框加到中间部分
        jFrame.add(center,BorderLayout.CENTER);

    }
}
